<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/5
 * Time: 9:45
 */

namespace app\sports_admin\model;
use think\Model;
use think\db;
use think\Request;
use app\sports_admin\model\TableTeacher;
class TableVideo extends Model
{
    protected $table = "table_video";
    protected $pk    = "id";
    protected $autoWriteTimestamp = true;
    public  function getIconPathAttr($values)
    {
        $data = "https://teacherxcx.tijian121.com".$values;
        return $data;
    }
    //关联表 table_teacher
    public function getTableTeacher()
    {
        return $this->belongsTo('TableTeacher','jsname','id')->field('id,title');
    }
    //关联地区表 table_region
    public function getRegion()
    {
        return $this->belongsTo('Region','city','id')->field('id,region_name');
    }
    public static function compilationVideo($page,$rows,$sort,$sortOrder,$searchName,$searchType)
    {
        $adminList = self::with('getTableTeacher')
            ->where(
                function($query)use($searchName,$searchType)
                {if($searchName){
                    $query->where('title','like',"%{$searchName}%");
                        }elseif($searchType){
                    $query->where('cat_id','=',"$searchType");
                    }elseif($searchName&&$searchType){
                    $query->where('title','like',"%{$searchName}%")->where('cat_id','=',"$searchType");
                }
                }
            )
            ->page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();
        $adminCount = self::with('getTableTeacher')
            ->where(
                function($query)use($searchName,$searchType)
                {if($searchName){
                    $query->where('title','like',"%{$searchName}%");
                }elseif($searchType){
                    $query->where('cat_id','=',"$searchType");
                }elseif($searchName&&$searchType){
                    $query->where('title','like',"%{$searchName}%")->where('cat_id','=',"$searchType");
                }
                }
            )
            ->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }

    //视频
    public static function gather($id)
    {
        return self::with("getTableTeacher,getRegion")->where('id',$id)->find();
    }
    //添加合集
    public static function addGather()
    {
        $message = Request::instance()->only(['title','icon_path','video_path','introduce','jsname','city','cat_id']);
        $introduce = htmlspecialchars($message['introduce']);
        return self::create(['title'=>$message['title'],
                             'icon_path'=>$message['icon_path'],
                             'video_path'=>$message['video_path'],
                             'introduce'=>$introduce,
                             'jsname'=>$message['jsname'],
                             'city'=>$message['city'],
                             'cat_id'=>$message['cat_id']]);
    }
    //删除合集
    public static function delGather($request)
    {
        $message = Request::instance()->only(['id']);
        return self::where('id',$message['id'])->delete();
    }
}